package csc.training.jsf2.view;

import java.io.Serializable;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;

import csc.training.jsf2.dao.Country_CityDaoImpl;
import csc.training.jsf2.entity.Account;
import csc.training.jsf2.service.CountryCityServiceImpl;
import csc.training.jsf2.service.LoginService;

/**
 * 
 * @author hdang20
 * 
 */
@ManagedBean
@SessionScoped
public class LoginManageBean implements Serializable {
	private static final long serialVersionUID = 3325780045480163859L;
	private String emailAddress;
	private String password;
	private String emailStatus;
	private String passwordStatus;
	private Account account = null;
	private boolean IsGoToProfile;
	private List<String> listCountry;
	private String selectedCountry;
	public String getNewPassword() {
		return newPassword;
	}

	public void setNewPassword(String newPassword) {
		this.newPassword = newPassword;
	}

	public String getNewPasswordConfirm() {
		return newPasswordConfirm;
	}

	public void setNewPasswordConfirm(String newPasswordConfirm) {
		this.newPasswordConfirm = newPasswordConfirm;
	}

	private List<String> listCity;
	private String newPassword;
	private String newPasswordConfirm;

	public List<String> getListCity() {
		return listCity;
	}

	public void setListCity(List<String> listCity) {
		this.listCity = listCity;
	}

	public String getSelectedCountry() {
		return selectedCountry;
	}

	public void setSelectedCountry(String selectedCountry) {
		this.selectedCountry = selectedCountry;
	}

	public List<String> getListCountry() {
		return listCountry;
	}

	public void setListCountry(List<String> listCountry) {
		this.listCountry = listCountry;
	}

	public boolean isIsGoToProfile() {
		return IsGoToProfile;
	}

	public void setIsGoToProfile(boolean isGoToProfile) {
		IsGoToProfile = isGoToProfile;
	}

	public Account getAccount() {
		return account;
	}

	public void setAccount(Account account) {
		this.account = account;
	}

	public void localeChanged(ValueChangeEvent e) {
		// assign new value to country
		selectedCountry = e.getNewValue().toString();
		CountryCityServiceImpl c = new CountryCityServiceImpl();
		listCity = c.getListCityByCountryName(selectedCountry);

	}

	@ManagedProperty(value = "#{loginServiceImpl}")
	private LoginService loginService;

	public String login() {
		// FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
		account = loginService.getAccount(emailAddress, password);
		if (account != null) {
			{
				if (IsGoToProfile == true) {
					CountryCityServiceImpl c = new CountryCityServiceImpl();
					listCountry = c.getListCountry();
					return "profiles";
				}
				return "mail_mgt";
			}
		} else {
			FacesContext context = FacesContext.getCurrentInstance();
			context.addMessage(
					null,
					new FacesMessage(
							"Your Email Address or Password is wrong, please try again!"));
			return "login";
		}
	}

	public String getEmailAddress() {
		return emailAddress;
	}

	public void setEmailAddress(String emailAddress) {
		if (loginService.checkEmailLogin(emailAddress)) {
			emailStatus = "<span style='color:blue;'><b> Succeed!</b></span>";
		} else {
			emailStatus = "<span style='color:red;'><b>Failed. Wrong Email!</b></span>";
		}
		this.emailAddress = emailAddress;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		if (loginService.checkPassword(password)) {
			passwordStatus = "<span style='color:blue;'><b>Succeed!</b></span>";
		} else {
			passwordStatus = "<span style='color:red;'><b>Failed. Wrong Password!</b></span>";
		}
		this.password = password;
	}

	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}

	public String getEmailStatus() {
		return emailStatus;
	}

	public void setEmailStatus(String emailStatus) {
		this.emailStatus = emailStatus;
	}

	public String getPasswordStatus() {
		return passwordStatus;
	}

	public void setPasswordStatus(String passwordStatus) {
		this.passwordStatus = passwordStatus;
	}

	private static BigDecimal getBigDecimalValue(Double d) {
		double doubleValue = 0.0;
		DecimalFormat decFormat = new DecimalFormat("#.##");
		if (d != null) {
			doubleValue = Double.parseDouble(decFormat.format(d));
		}
		return new BigDecimal(doubleValue);
	}

	public static void main(String[] args) {
		Double value = 0.08250000000000000388578058618804789148271083831787109375;
		BigDecimal decimalValue = getBigDecimalValue(value);
		System.out.print(decimalValue.doubleValue());
	}

}
